package net.csdra.libraryborrowing.service.book;

import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by msf on 2017/10/16.
 */

@Component
public class BookCopyDispatchComponentImpl implements BookCopyDispatchComponent {

    List<String> activeBookCopys;

    public BookCopyDispatchComponentImpl() {
        this.activeBookCopys = new ArrayList<>();
    }

    @Override
    public boolean getBookCopyIsActive(String bookCopyId) {
        synchronized (activeBookCopys) {
            for (int i = 0; i < this.activeBookCopys.size(); i++) {
                if (bookCopyId.equals(this.activeBookCopys.get(i))) {
                    return false;
                }
            }
            this.activeBookCopys.add(bookCopyId);
            return true;
        }
    }

    @Override
    public void setBookCopyActiveIsTrue(String bookCopyId) {
        this.activeBookCopys.remove(bookCopyId);
    }


}
